/*
* @Author: Adolphus.Yao
* @Date:   2019-07-23 17:17:49
* @Last Modified by:   Adolphus.Yao
* @Last Modified time: 2019-07-23 17:29:18
*/
package main

import (
    "flag"
    "bufio"
    "fmt"
    "io"
    "os"
    "strings"
    "encoding/json"
    "time"
    "yss.dgd/pb/service/plstore"
)

const (
    CARD_STATUS_VALID   = 0
    CARD_STATUS_INVALID = 1
)

var (
    h bool
    file_path string
    ctype uint64
    dd bool
)

func init() {
    flag.BoolVar(&h, "h", false, "this help")
    flag.StringVar(&file_path, "f", "out.txt", "set json datas file")
    flag.Uint64Var(&ctype, "t", 0, "set card type for search")
    flag.BoolVar(&dd, "d", false, "是否进行重复条件的加入查询检查")

    // 改变默认的 Usage，flag包中的Usage 其实是一个函数类型。这里是覆盖默认函数实现，具体见后面Usage部分的分析
    flag.Usage = usage
}

func main() {
    flag.Parse()

    if h {
        flag.Usage()
    } else {
        fi, err := os.Open(file_path)
        if err != nil {
            fmt.Printf("Error: %s\n", err)
            return
        }
        defer fi.Close()

        br := bufio.NewReader(fi)
        lineNo := 0
        for {
            lineNo++
            line, err := br.ReadString('\n')
            line = strings.TrimSpace(line)
            if err != nil {
                if io.EOF == err {   //读取结束，会报EOF
                    break
                }
                fmt.Println(err, " err!!!!!!!!!! ", lineNo)
                continue
            }
            lineLen := len(line)
            if 0 < lineLen && strings.HasPrefix(line, "[yao]  c") {
                jIndex := strings.Index(line, "{")
                if 0 < jIndex {
                    jsonStr := line[jIndex:lineLen]
                    checkJson(jsonStr, line, lineNo)
                } else {
                    fmt.Println("Error line can not find json char!", line, " ", lineNo)
                }
            } else {
                fmt.Println("Error line is invaild data!", line, " ", lineNo)
            }
        }
    }
}

func usage() {
    fmt.Fprintf(os.Stderr, `jcheck version: jcheck/1.0.0
Usage: jcheck

Options:
`)
    flag.PrintDefaults()
}

func checkJson(jsonStr, line string, lineNo int) {
    var alradyBindCardList plstore.CardInfoList
    //使用 json.Unmarshal(data []byte, v interface{})进行转换,返回 error 信息
    if err := json.Unmarshal([]byte(jsonStr), &alradyBindCardList); err != nil {
        fmt.Println(err, "        ", jsonStr, " err!!!!!!!!!! ", lineNo)
        return
    }
    loc, _ := time.LoadLocation("Asia/Shanghai")        //设置时区
    tt, _ := time.ParseInLocation("2006-01-02 15:04:05", "2019-10-19 00:00:00", loc)
    st := uint32(tt.Unix())
    for i := 0; i < len(alradyBindCardList.CardList); i++ {
        iCardInfo := alradyBindCardList.CardList[i]
        if iCardInfo.Status == CARD_STATUS_VALID {
            if st < iCardInfo.CreateTime && uint32(ctype) == iCardInfo.CardType {
                if dd {
                    //检查此卡是否存在多张
                    have := false
                    for j := 0; j < len(alradyBindCardList.CardList); j++ {
                        if i == j {
                            continue
                        }
                        jCardInfo := alradyBindCardList.CardList[j]
                        if jCardInfo.Status == CARD_STATUS_VALID {
                            if jCardInfo.CardType == iCardInfo.CardType {
                                if uint32(10) == jCardInfo.CardType || uint32(4) == jCardInfo.CardType {
                                    if jCardInfo.PlateNumber == iCardInfo.PlateNumber {
                                        have = true
                                        break;
                                    }
                                } else {
                                    if 0 < len(jCardInfo.LicenseCode) && 0 < len(iCardInfo.LicenseCode) {
                                        if jCardInfo.LicenseCode == iCardInfo.LicenseCode {
                                            have = true
                                            break;
                                        }
                                    } else {
                                        if len(jCardInfo.CardNumber) == len(iCardInfo.CardNumber) {
                                            ifex3 := true
                                            if 0 < len(jCardInfo.CityCode) && 0 < len(iCardInfo.CityCode) {
                                                ifex3 = jCardInfo.CityCode == iCardInfo.CityCode
                                            }
                                            ifex4 := strings.Contains(jCardInfo.CardNumber, "**")
                                            ifex5 := jCardInfo.CardNumber[0] == iCardInfo.CardNumber[0]
                                            ifex6 := jCardInfo.CardNumber[len(jCardInfo.CardNumber) - 1] == iCardInfo.CardNumber[len(iCardInfo.CardNumber) - 1]
                                            if (jCardInfo.CardNumber == iCardInfo.CardNumber || (ifex4 && ifex5 && ifex6)) && ifex3 {
                                                have = true
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if have {
                        fmt.Println(line, " > ", lineNo)
                    }
                } else {
                    fmt.Println(line, " > ", lineNo)
                }
            }
        }
    }
}
